package com.workstudy.wstudy.service.impl;

import java.util.List;

import com.workstudy.common.annotation.DataScope;
import com.workstudy.common.core.domain.entity.SysUser;
import com.workstudy.common.utils.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.workstudy.wstudy.mapper.WstMessageMapper;
import com.workstudy.wstudy.domain.WstMessage;
import com.workstudy.wstudy.service.IWstMessageService;
import com.workstudy.common.core.text.Convert;

/**
 * 消息管理Service业务层处理
 * 
 * @author sun
 * @date 2021-04-04
 */
@Service
public class WstMessageServiceImpl implements IWstMessageService 
{
    @Autowired
    private WstMessageMapper wstMessageMapper;

    @Autowired
    private WebSocketServiceImpl socketService;

    /**
     * 查询消息管理
     * 
     * @param id 消息管理ID
     * @return 消息管理
     */
    @Override
    public WstMessage selectWstMessageById(Long id)
    {
        return wstMessageMapper.selectWstMessageById(id);
    }


    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<WstMessage> selectWstMessageList(WstMessage wstMessage)
    {
        return wstMessageMapper.selectWstMessageList(wstMessage);
    }

    /**
     * 新增消息管理
     * 
     * @param wstMessage 消息管理
     * @return 结果
     */
    @Override
    public int insertWstMessage(WstMessage wstMessage)
    {
        wstMessage.setCreateTime(DateUtils.getNowDate());
        return wstMessageMapper.insertWstMessage(wstMessage);
    }

    /**
     * 修改消息管理
     * 
     * @param wstMessage 消息管理
     * @return 结果
     */
    @Override
    public int updateWstMessage(WstMessage wstMessage)
    {
        wstMessage.setUpdateTime(DateUtils.getNowDate());
        return wstMessageMapper.updateWstMessage(wstMessage);
    }

    /**
     * 删除消息管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWstMessageByIds(String ids)
    {
        return wstMessageMapper.deleteWstMessageByIds(Convert.toStrArray(ids));
    }

    @Override
    public int sendMessage(WstMessage message) {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        message.setCreateUserId(loginUser.getUserId());
        message.setPubilshUserId(loginUser.getUserId());
        message.setCreateBy(loginUser.getUserName());
        message.setCreateTime(DateUtils.getNowDate());
        message.setIsDelete(0);
        int row =  wstMessageMapper.insertWstMessage(message);
        socketService.sendMessage(message.getReceiveUserId().toString(),"new");
        return row;
    }

    @Override
    public int setRead(WstMessage message) {
        int row = wstMessageMapper.updateWstMessage(message);
        WstMessage params = wstMessageMapper.selectWstMessageById(message.getId());
        socketService.sendMessage(params.getReceiveUserId().toString(),"changeCount");
        return row;
    }

    /**
     * 删除消息管理信息
     * 
     * @param id 消息管理ID
     * @return 结果
     */
    @Override
    public int deleteWstMessageById(Long id)
    {
        return wstMessageMapper.deleteWstMessageById(id);
    }
}
